package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
   @Autowired
   private SetmealMapper setmealMapper;


   @Override
   public List<Setmeal> selectSetmealsWithEableStatusByCategoryId(Setmeal setmeal) {
      List<Setmeal> setmealsWithEableStatusByCategoryId = setmealMapper.getSetmealsWithEableStatusByCategoryId(setmeal);
        return setmealsWithEableStatusByCategoryId;
   }

   @Override
   public List<DishItemVO> selectDishItemVOBySetmealId(Long setmealId) {
      List<DishItemVO> dishItemVOS = setmealMapper.getDishItemVOBySetmealId(setmealId);
      return dishItemVOS;
   }

   @Override
   public void updateSetmealStatus(Integer status,Integer id) {
      Integer notSaleDishCountsAssociatedSetmeal = setmealMapper.findNotSaleDishCountsAssociatedSetmeal(id);
      if(notSaleDishCountsAssociatedSetmeal > 0){
         throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
      }
      Setmeal setmeal = Setmeal.builder().id(id.longValue()).status(status).build();
      setmealMapper.updateSetmealStatus(setmeal);
   }

   @Override
   @Transactional
   public void saveSetmeal(SetmealDTO setmealDTO) {
      Setmeal setmeal = Setmeal.builder().build();
      BeanUtils.copyProperties(setmealDTO,setmeal);
      setmealMapper.saveSetmeal(setmeal);
      Long setmealId = setmeal.getId();
      List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
      setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
      setmealMapper.saveSetmealDishItemsByBatches(setmealDishes);
   }

   @Override
   @Transactional
   public SetmealVO getSetmealWithDishById(Integer id) {
      Setmeal setmeal = setmealMapper.getSetmealById(id);
      SetmealVO setmealVO = SetmealVO.builder().build();
      BeanUtils.copyProperties(setmeal,setmealVO);
      List<SetmealDish> setmealWithDishsById = setmealMapper.getSetmealWithDishsById(id);
      setmealVO.setSetmealDishes(setmealWithDishsById);
      return setmealVO;
   }

   @Override
   @Transactional
   public void deleteSetmealsByBatch(List<Integer> ids) {
      //判断是否存在起售中的套餐
      Integer setmealsStatusByBatch = setmealMapper.findSetmealsStatusByBatch(ids);

      if(setmealsStatusByBatch > 0){
         throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
      }

      //删除套餐及其关联的菜品
      setmealMapper.deleteSetmealByBatch(ids);
      setmealMapper.deleteSetmealDishByBatch(ids);
   }

   @Override
   @Transactional
   public void updateSetmeal(SetmealDTO setmealDTO) {
      Setmeal setmeal = Setmeal.builder().build();
      BeanUtils.copyProperties(setmealDTO,setmeal);
      setmealMapper.updateSetmeal(setmeal);
      Long id = setmealDTO.getId();
      setmealMapper.deleteSetmealDishBySetmealId(id);
      List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
      setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(id));
      setmealMapper.saveSetmealDishItemsByBatches(setmealDishes);
   }

   @Override
   public PageResult getSetmealPage(SetmealPageQueryDTO setmealPageQueryDTO) {
      PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
      Page<SetmealVO> setmealPage = setmealMapper.getSetmealPage(setmealPageQueryDTO);
      return new PageResult(setmealPage.getTotal(), setmealPage.getResult());
   }
}
